package cn.nsu.douban.web.action;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.hibernate.mapping.Array;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.nsu.douban.entity.ActorInfo;
import cn.nsu.douban.entity.EvaluationInfo;
import cn.nsu.douban.entity.MovieInfo;
import cn.nsu.douban.entity.User;
import cn.nsu.douban.service.IActorService;
import cn.nsu.douban.service.IMovieService;
import cn.nsu.douban.service.IPingFenService;
import cn.nsu.douban.service.IUserService;
import cn.nsu.utils.FormateUtis;

@Controller
@Scope("prototype")
public class MovieAction extends ActionSupport {
	static String imageRootPat="http://localhost:8080/dy/";
	//上传
	public File file;
	public String fileFileName;
    public  String  fileContentType;	
	
	
	public String date;
	// 电影id
	public Integer mid;

	// 当前页码
	public Integer pageNum = 0;
	// 每页显示数据
	public static Integer pageSize = 4;
	// 总也数
	public Integer totalPage;
	// 总条数
	public Integer totalNum;

	// 搜索文本
	public String search_text;
    //修改后的数据
	public MovieInfo mi=new MovieInfo();
	// 压栈
	public User nowUser;
    
	
	@Resource
	private IMovieService iMovieService;
	@Resource
	private IActorService iActorService;
	@Resource
	private IUserService iUserService;
	@Resource
	private IPingFenService iPingFenService;
	// ajax
	public List<MovieInfo> movieList;

	public List<MovieInfo> getMovieList() {
		return movieList;
	}

	public void setMovieList(List<MovieInfo> movieList) {
		this.movieList = movieList;
	}

	// 评分属性驱动
	public Integer pf;
	// 电影ID
	public String moid;

	// 内容
	public String content;
	/**
	 * -------------------------------------------------------------
	 */
	//admin
	  public String delete(){
		  MovieInfo mInfo = iMovieService.getByMid(mid);
		  iMovieService.deleteMovie(mInfo);
		  return "to_admin_ViewMovie";
	  }
	  
		//后台详细
		public String showMovieInfo(){
			  MovieInfo movieInfo = iMovieService.getByMid(mid);
	          ServletActionContext.getRequest().setAttribute("mi", movieInfo);
			   return "adminMovieInfo";
		}
   //修改
	public String update() throws IOException{
		Date updateDate = FormateUtis.formateDate(date);
		mi.setDate(updateDate);
		String fileType = fileFileName.substring(fileFileName.lastIndexOf("."));
		String fileName = UUID.randomUUID().toString().replace("-", "");
		String newFileName=fileName+fileType;
		String path="E:\\xuni\\"+newFileName;
		File files=new File(path);
		FileUtils.copyFile(file, files);
		mi.setImagpath("http://localhost:8080/dy/"+newFileName);
         iMovieService.updateMovieInfo(mi);	
         
         return "to_admin_ViewMovie";
	}
	
	public String  ids;
	//批量删除
	public void deleteAll(){
		String[] charInt=ids.split(",");
		iMovieService.deleteMovieByDuoId(charInt);
	}
	
	//管理员查询所有电影
	public String AllMovieInfo(){
		List<MovieInfo> mList = iMovieService.getAll();
		ServletActionContext.getContext().getValueStack().set("mList",mList);
		return "adminList";
	}
	/**
	 * -------------------------------------------------------------
	 */
	
	
	
	// qbc查询
	public String serarchMovie() throws UnsupportedEncodingException {
		// 中文ajax转码
		movieList = iMovieService.getMovieInfoByName(URLDecoder.decode(search_text, "UTF-8"));
		return "ajaxGetMovieInfoByName";
	}

	public String showisHotMovie() {
		nowUser = (User) ServletActionContext.getRequest().getSession().getAttribute("USER");
		ActionContext.getContext().getValueStack().set("USERS", nowUser); // 压
		totalNum = iMovieService.getMoveiIsHotCount();
		totalPage = totalNum % pageSize > 0 ? totalNum / pageSize + 1 : totalNum / pageSize;
		if (pageNum == 0) {
			pageNum = 1;
		}
		//保存计算后的星星评分
		List<MovieInfo> countNowList=new ArrayList<MovieInfo>();
		List<MovieInfo> mList = iMovieService.getAll(pageNum, pageSize);
		for (MovieInfo movieInfo : mList) {
		   Integer movieCount=iPingFenService.getPingFenCount(movieInfo);
		   if(movieCount==0){
			   movieCount=1;
		   }
		   int scoreCount=Math.abs(movieInfo.getScore()/movieCount);
		   if(scoreCount>5){
			   scoreCount=5;
		   }
           MovieInfo movieInfoNow=new MovieInfo();
           movieInfoNow.setScore(scoreCount);
           movieInfoNow.setAc(movieInfo.getAc());
           movieInfoNow.setBianju(movieInfo.getBianju());
           movieInfoNow.setId(movieInfo.getId());
           movieInfoNow.setImagpath(movieInfo.getImagpath());
           movieInfoNow.setInfo(movieInfo.getInfo());
           movieInfoNow.setDirector(movieInfo.getDirector());
           movieInfoNow.setDate(movieInfo.getDate());
           movieInfoNow.setIshow(movieInfo.getIshow());
           movieInfoNow.setName(movieInfo.getName());
           countNowList.add(movieInfoNow);
		}
		ActionContext.getContext().getValueStack().set("mList", countNowList);
		return "main";
	}

	
	
	// 显示电影详细
	public String showMovieDetail() {
		nowUser = (User) ServletActionContext.getRequest().getSession().getAttribute("USER");
		ActionContext.getContext().getValueStack().set("USERS", nowUser); 
		MovieInfo movieInfo = iMovieService.getMovieInfoById(mid);
		ActionContext.getContext().getValueStack().set("mi", movieInfo);
		ActorInfo actorInfo = iActorService.getActorByMovieId(movieInfo);
		ActionContext.getContext().put("am", actorInfo);
		emInfoList = iPingFenService.getNowPingFen(movieInfo);
		return "toDetail";
	}

	// 评分
	public String mark() {
		// 缓存修改数据
		iMovieService.updateMovieInfoById(moid, pf);
		User loginUser = (User) ServletActionContext.getRequest().getSession().getAttribute("USER");
		EvaluationInfo emInfo = new EvaluationInfo();
		emInfo.setContent(content);
		MovieInfo movieInfo = new MovieInfo();
		movieInfo.setId(Integer.parseInt(moid));
		emInfo.setUser(loginUser);
		emInfo.setMovieInfo(movieInfo);
		emInfo.setScore(pf);
		emInfo.setTime(new Date());
		iPingFenService.insertPingFen(emInfo);
		return "to_detail";
	}

	// ajax
	public String timeQuery() {
		movieList = iMovieService.findMovieByDate();
		return "ajaxGetMovieInfoByDate";
	}

	public void setiMovieService(IMovieService iMovieService) {
		this.iMovieService = iMovieService;
	}

	public Integer getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}

	public Integer getTotalNum() {
		return totalNum;
	}

	public void setTotalNum(Integer totalNum) {
		this.totalNum = totalNum;
	}

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	public User getNowUser() {
		return nowUser;
	}

	public void setNowUser(User nowUser) {
		this.nowUser = nowUser;
	}

	public void setMoid(String moid) {
		this.moid = moid;
	}

	public String getMoid() {
		return moid;
	}
	public List<EvaluationInfo> emInfoList;

	public List<EvaluationInfo> getEmInfoList() {
		return emInfoList;
	}

}
